/**
 * created by 小卷毛, 2020/10/21
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.util

import android.annotation.SuppressLint
import android.content.Context
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.view.animation.AnimationUtils
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import com.sgcc.pda.mdrh.task.base.BaseDialog
import com.sgcc.pda.mdrh.task.common_ui.base.inflate
import com.sgcc.pda.mdrh.task.common_ui.base.oneClick
import com.sgcc.pda.mdrh.task.monitor.R
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * 描 述：
 * 作 者：小卷毛  2020-10-21 15:57
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
fun Context.showHintDialog(
    title: String = "提示",
    hint: String = "",
    cancel: String = "否",
    sure: String = "是",
    isForced: Boolean = false,
    onCancel: () -> Unit = {},
    onSure: () -> Unit = {}
) {
    val dialog = object : BaseDialog(this) {

        override fun onCreateView(): View {
            widthScale(0.78f)
            val view = inflate<View>(R.layout.dialog_remind_hint)

            val tvTitle = view.findViewById<TextView>(R.id.dialog_title)
            val tvHint = view.findViewById<TextView>(R.id.dialog_hint)
            val btCancel = view.findViewById<TextView>(R.id.dialog_cancel)
            val btSure = view.findViewById<TextView>(R.id.dialog_sure)

            tvTitle.text = title
            tvHint.text = hint
            btCancel.text = cancel
            btSure.text = sure

            btCancel.oneClick {
                dismiss()
                onCancel.invoke()
            }

            btSure.oneClick {
                dismiss()
                onSure.invoke()
            }

            return view
        }
    }

    dialog.setOnKeyListener { _, keyCode, _ ->
        if (keyCode == KeyEvent.KEYCODE_BACK) return@setOnKeyListener isForced
        else return@setOnKeyListener false
    }
    dialog.setCanceledOnTouchOutside(!isForced)
    dialog.show()
}

fun Context.showConnectDialog(hint: String = "蓝牙连接中...") : BaseDialog {
    val dialog = object : BaseDialog(this) {

        private lateinit var ivImg: ImageView
        private lateinit var tvHint: TextView

        override fun onCreateView(): View {
            widthScale(0.0f)

            val view = inflate<View>(R.layout.dialog_connect_hint)
            ivImg = view.findViewById(R.id.dialog_img)
            tvHint = view.findViewById(R.id.dialog_hint)
            tvHint.text = hint

            val rotateAnimation = AnimationUtils.loadAnimation(
                this@showConnectDialog,
                R.anim.anim_rotate
            )
            rotateAnimation.interpolator = LinearInterpolator()
            ivImg.startAnimation(rotateAnimation)

            return view
        }

        @SuppressLint("CheckResult")
        override fun onSuccess(done: String) {
            ivImg.clearAnimation()
            ivImg.setImageResource(R.mipmap.icon_right)
            tvHint.text = done

            Completable.timer(1000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { dismiss() }
        }
    }

    dialog.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
    dialog.setCanceledOnTouchOutside(false)
    dialog.setCancelable(false)
    dialog.show()

    return dialog
}